import requests
import parsel
from lxml import etree
import csv

f = open('shuju.csv', mode='w', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
            '期号',
            '红球号码1',
            '红球号码2',
            '红球号码3',
            '红球号码4',
            '红球号码5',
            '红球号码6',
            '蓝球',
            '奖池奖金',
            '一等奖注数',
            '一等奖奖金',
            '二等奖注数',
            '二等奖奖金',
            '总投注额',
            '开奖日期',
])
csv_writer.writeheader()    # 写入表头
# csv_writer = csv.writer(f)

url = 'https://datachart.500.com/ssq/history/history.shtml'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}
resp = requests.get(url=url, headers=headers)

# selector = parsel.Selector(resp.text)
# tds = selector.css('.t_tr1').getall()
et = etree.HTML(resp.text)
trs = et.xpath('//*[@id="tdata"]/tr[@class="t_tr1"]')
# print(len(tds))
for item in trs:
    number = item.xpath('./td[1]/text()')[0]
    red_number_1 = item.xpath('./td[2]/text()')[0]
    red_number_2 = item.xpath('./td[3]/text()')[0]
    red_number_3 = item.xpath('./td[4]/text()')[0]
    red_number_4 = item.xpath('./td[5]/text()')[0]
    red_number_5 = item.xpath('./td[6]/text()')[0]
    red_number_6 = item.xpath('./td[7]/text()')[0]
    blue_ball = item.xpath('./td[8]/text()')[0]
    prize_pool_bonuses = item.xpath('./td[10]/text()')[0].replace(',', '')
    nofpb = item.xpath('./td[11]/text()')[0]
    fpp = item.xpath('./td[12]/text()')[0].replace(',', '')
    spb = item.xpath('./td[13]/text()')[0]
    spp = item.xpath('./td[14]/text()')[0].replace(',', '')
    total_stake = item.xpath('./td[15]/text()')[0].replace(',', '')
    draw_date = item.xpath('./td[16]/text()')[0]

    dic = {
        '期号': number,
        '红球号码1': red_number_1,
        '红球号码2': red_number_2,
        '红球号码3': red_number_3,
        '红球号码4': red_number_4,
        '红球号码5': red_number_5,
        '红球号码6': red_number_6,
        '蓝球': blue_ball,
        '奖池奖金': prize_pool_bonuses,
        '一等奖注数': nofpb,
        '一等奖奖金': fpp,
        '二等奖注数': spb,
        '二等奖奖金': spp,
        '总投注额': total_stake,
        '开奖日期': draw_date
    }
    csv_writer.writerow(dic)
    print(number, red_number_1, red_number_2, red_number_3, red_number_4,
          red_number_5, red_number_6, blue_ball, prize_pool_bonuses,
          nofpb, fpp, spb, spp, total_stake, draw_date)
